package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
//解决跨域问题注解。
@CrossOrigin
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private TokenDecode tokenDecode;
    /**
     * 添加购物车
     * @param skuId
     * @param num
     * @return
     */
    @RequestMapping("/add")
//   客户端传递 skuid   购买商品数量 num
    public Result add(@RequestParam("skuId") String skuId,@RequestParam("num") Integer num){
        //用户名暂时静态定义
//        String username="itheima";
//        用户名从令牌中动态获取
        String username = tokenDecode.getUserInfo().get("username");
        //調用service层方法 add 传入参数
        cartService.add(skuId,num,username);
        return new Result(true, StatusCode.OK,"加入购物车成功");
    }
    /***
     * 查询用户购物车列表
     * @return
     */
    @GetMapping("/list")
    public Map list(){
        //暂时静态，后续修改
//        String username = "itheima";
        //通过令牌动态获取用户名
        String username = tokenDecode.getUserInfo().get("username");

        return cartService.list(username);
    }
}
